package com.cn.ryc.service;

import com.cn.ryc.mapper.AlarmLevelMapper;
import com.cn.ryc.mapper.AlarmMapper;
import com.cn.ryc.mapper.AlarmTypeMapper;
import com.cn.ryc.model.Alarm;
import com.cn.ryc.util.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;


@Service
public class AlarmService {

    @Autowired
    private AlarmMapper alarmMapper;

    @Autowired
    private AlarmTypeMapper alarmTypeMapper;

    @Autowired
    private AlarmLevelMapper alarmLevelMapper;


    public ResponseEntity<ApiResponse<List<Alarm>>> getAllAlarms() {
        try {
            List<Alarm> alarms = alarmMapper.findAllAlarms();
            return ResponseEntity.ok(ApiResponse.success(alarms));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(ApiResponse.error(500, null));
        }
    }

    public ResponseEntity<ApiResponse<String>> updateAlarmStatus(Long id, Boolean processed) {
        try {
            Alarm alarm = alarmMapper.findById(id);
            if (alarm == null) {
                return ResponseEntity.status(404).body(ApiResponse.error(404, "报警记录不存在"));
            }
            
            LocalDateTime processTime = processed ? LocalDateTime.now() : null;
            alarmMapper.updateAlarmStatus(id, processed, processTime);
            
            return ResponseEntity.ok(ApiResponse.success("更新成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(ApiResponse.error(500, "服务器内部错误"));
        }
    }

// 返回一个ResponseEntity对象，包含一个ApiResponse对象，该对象包含一个List<String>类型的参数
    public ResponseEntity<ApiResponse<List<String>>> getAlarmTypes() {

            // 调用alarmTypeMapper的findAllNames方法，获取所有报警类型的名称
            List<String> types = alarmTypeMapper.findAllNames();
            // 返回一个包含所有报警类型名称的ApiResponse对象
            return ResponseEntity.ok(ApiResponse.success(types));

    }

    public ResponseEntity<ApiResponse<List<String>>> getAlarmLevels() {

            List<String> levels = alarmLevelMapper.findAllNames();
            return ResponseEntity.ok(ApiResponse.success(levels));

    }


    public ResponseEntity<ApiResponse<String>> insertAlarmInfo(Map<String, Object> request) {
        try {
            // 从请求中获取参数
            String type = (String) request.get("type");
            String level = (String) request.get("level");
            String description = (String) request.get("description");
            String videoUrl = (String) request.get("video_url");
            
            // 创建新的警报对象
            Alarm alarm = new Alarm();
            alarm.setTime(LocalDateTime.now()); // 使用当前时间
            alarm.setType(type);
            alarm.setLevel(level);
            alarm.setVideoUrl(videoUrl);
            alarm.setDescription(description);
            alarm.setProcessed(false); // 默认未处理
            alarm.setCreatedAt(LocalDateTime.now());
            
            // 插入警报信息
            alarmMapper.insertAlarm(alarm);
            
            return ResponseEntity.ok(ApiResponse.success("警报信息添加成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body(ApiResponse.error(500, "服务器内部错误：" + e.getMessage()));
        }
    }
}